var mubang = document.querySelector("#stick");
mubang.addEventListener("click", clickMubang, false);

var muyu = document.querySelector("#muyu");
muyu.addEventListener("click", clickMuyu, false);

var music = document.getElementById("music");
var auto = document.querySelector("#auto");
var speedup = document.querySelector("#speedup");

// 定时事件实例
// 自动播放音乐
var autoPlayMusic;
var autoAddNum;
// 改变checkbox时触发
auto.addEventListener("change", () => {
    clearInterval(autoPlayMusic);
    clearInterval(autoAddNum);
    if(speedup.checked&&auto.checked){
        quickPlay();
        quickAddNum();//
        setTimeout(() => {
          playMusic();
          autoPlayMusic = setInterval(playMusic, 200);//每隔一秒播放音效
          autoAddNum = setInterval(quickAddNum, 200);
        }, 100);
    }else 
  if (auto.checked) {
    autoPlay();
    setTimeout(() => {
        addNum();
      playMusic();
      autoPlayMusic = setInterval(playMusic, 1000);//每隔一秒播放音效
      autoAddNum = setInterval(addNum, 1000);
    }, 500);
  } else {
    clearInterval(autoPlayMusic);
    clearInterval(autoAddNum);
    document.querySelector(".stick").className = "stick";
  }
});

// 加速
speedup.addEventListener("change", () => {
    clearInterval(autoPlayMusic);
    clearInterval(autoAddNum);
    if (auto.checked&&speedup.checked) {
      quickPlay();
      quickAddNum();
      setTimeout(() => {
        playMusic();
        autoPlayMusic = setInterval(playMusic, 200);//每隔一秒播放音效
        autoAddNum = setInterval(quickAddNum, 200);
      }, 100);
    }
    else if(auto.checked){
        clearInterval(autoPlayMusic);
        clearInterval(autoAddNum);
        autoPlay();
        addNum();
        setTimeout(() => {
          playMusic();
          autoPlayMusic = setInterval(playMusic, 1000);//每隔一秒播放音效
          autoAddNum = setInterval(addNum, 1000);
        }, 500);
    }
     else {
      clearInterval(autoPlayMusic);
      clearInterval(autoAddNum);
      document.querySelector(".stick").className = "stick";
    }
  });

// 点击木棒
function clickMubang() {
  document.querySelector(".stick").className = "stick";
  window.requestAnimationFrame(function (time) {
    window.requestAnimationFrame(function (time) {
      document.querySelector(".stick").className = "stick move";
    });
  });
  setTimeout(playMusic, 200);
  setTimeout(addNum, 300);
}

// 点击木鱼
function clickMuyu() {
    document.querySelector(".muyu").className = "muyu";
        window.requestAnimationFrame(function (time) {
            document.querySelector(".muyu").className = "muyu muyuscale";
        });
    playMusic();
    addNum();
  }

// 播放音乐
function playMusic() {
  music.currentTime = 0;
  music.play();
}

// 自动敲打木鱼
function autoPlay() {
  document.querySelector(".stick").className = "stick";
  window.requestAnimationFrame(function (time) {
    window.requestAnimationFrame(function (time) {
      document.querySelector(".stick").className = "stick auto_move";
    });
  });
}

function quickPlay() {
    document.querySelector(".stick").className = "stick";
    window.requestAnimationFrame(function (time) {
      window.requestAnimationFrame(function (time) {
        document.querySelector(".stick").className = "stick quick_move";
      });
    });
  }

// 今日功德,总功德
var allNum = 0;
var all = document.querySelector("#all_count");
// 初始化获取浏览器保存的功德
getNum();
function getNum() {
  if (localStorage.key("allNum")) {
    allNum = +localStorage.getItem("allNum");
    all.innerHTML = allNum;
  } else {
    localStorage.setItem("allNum", 0);
  }
}

function addNum() {
    tipsPlay();
  // 累计已攒功德+1
  allNum = +allNum + 1;
  all.innerHTML = allNum;
  localStorage.setItem("allNum", allNum);
}

function quickAddNum() {
    tipsPlay();
    allNum = +allNum + 1;
    all.innerHTML = allNum;
    localStorage.setItem("allNum", allNum);
  }

// 显示功德+1的动画
var tips = document.querySelector("#tips");
function tipsPlay(){
    document.querySelector("#tips").className = "tips";
    window.requestAnimationFrame(function (time) {
        document.querySelector("#tips").className = "tips_animation";
    });
}
